import express from "express";
import {getCardBacks, getCards} from "../utils/hsapi.js";
import Result from "../domain/R.js";
import logger from "log4js";
import {HsCardBacksCategory, MinionTypes as minionTypes, keywords} from "../utils/HsParms.js";


const log = logger.getLogger('page-card-backs');
const router = express.Router();

router.get('/', async function (req, res, next) {
    let result = new Result()
    res.render('index', result);
})

router.get('/battlegrounds', async function (req, res, next) {

    log.info(req.url)
    let result = new Result('炉石酒馆战旗')
    result.setPath(req.url)
    result.setData({minionTypes: minionTypes, keywords})
    result.setJsList(['/js/unlazy.iife.js']);
    log.info(minionTypes)
    try {
        res.render('battlegrounds', result)
    } catch (err) {
        log.error(err);
        result.setCode(-1)
        res.render('battlegrounds', result)
    }

})

router.get('/cardbacks', async function (req, res, next) {
    let result = new Result('炉石卡背预览')
    result.setJsList(['/js/unlazy.iife.js']);
    let queryParam = {
        locale: "zh_CN", pageSize: 999, textFilter: null, cardBackCategory: null
    }
    if (req.query.textFilter) {
        queryParam.textFilter = req.query.textFilter
    }
    if (req.query.cardBackCategory) {
        queryParam.cardBackCategory = req.query.cardBackCategory
    }
    result.setPath(req.url)
    try {
        const ret = await getCardBacks(queryParam)
        result.setData({ret, HsCardBacksCategory})
        result.setQueryParam(queryParam)
        res.render('cardbacks', result)
    } catch (err) {
        log.error(err);
        result.setCode(-1)
        res.render('cardbacks', result)
    }

})


export default router;

